<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BackController extends Controller
{
    //显示页面
    public function back(){
    	return view('home.back');
    }

    //执行找回密码
    public function doback(Request $request){
    	// dd($request->all());
    	//检测验证码是否正确
        $code = $request->input('code');
        $scode = session('code');
        // if($code != $scode){
        //     return back()->with('error','验证码不正确');
        // }

        $data = $request->only(['phone']);
        $data['password'] = Hash::make($request->input('password'));
        $data['token'] = str_random(50);
        $phone = $data['phone'];
        $pass = $data['password'];
        // dd($phone);
        $res = DB::table('user')->where('phone',$phone)->update(['password'=>$pass]);	
        if($res){
        	// echo '找回密码成功';
        	return redirect('/login')->with('success','找回密码成功');
        }else{
        	return redirect('/login')->with('error','找回密码失败');
        }
    }


    //接收ajax请求 发送验证码
    public function sendMess(Request $request){
        //提取手机号
        $phone = $request->input('phone');
        //随机验证码
        $code = rand(1000,9999);
        //将验证码存入session
        session(['code'=>$code]);
        $res = $this->sendmessage($phone,$code);
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    // 发送短信验证
    public function sendmessage($to,$code){
        //初始化必填
        $options['accountsid']='54ed35c0f4165c3bfb18dd928ce8b34d';
        $options['token']='acf137ffb5d0a589b7bf8ed7e2e2d7fd';


        //初始化 $options必填
        $ucpass = new \Ucpaas($options);

        //开发者账号信息查询默认为json或xml

        /* echo $ucpass->getDevinfo('xml'); */

        //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
        $appId = "0ad4a1d8b9cf4ce88888046ef39c28b5";//应用ID
        // $to = "13203836651";//发送给谁
        $templateId = "31575";//短信模板ID
        // $code = rand(1000,9999);
        $param="MEI网,".$code.",1";

        $res = $ucpass->templateSMS($appId,$to,$templateId,$param);

        $arr = json_decode($res,true);
        if($arr['resp']['respCode'] === '000000'){
            return true;
        }else{
            return false;
        }
    }
}
